package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.*;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Reference
    private AddressService addressService;

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile){
        //获得图片的后缀
       try{
           String originalFilename = multipartFile.getOriginalFilename();
           String substring = originalFilename.substring(originalFilename.lastIndexOf("."));

           //起名
           String filename = UUID.randomUUID().toString()+substring;

           //加载到七牛云中
           QiniuUtils.upload2Qiniu(multipartFile.getBytes(),filename);

           //存放入rides中
           Jedis resource = jedisPool.getResource();
           resource.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,filename);
           resource.close();
           //
           //返回结果
           return new Result(true , MessageConstant.PIC_UPLOAD_SUCCESS , filename);
       }catch (Exception e){
           e.printStackTrace();
           return new Result(false , MessageConstant.PIC_UPLOAD_FAIL);
       }

    }

    //添加
    @RequestMapping("/add")
    public Result add(@RequestBody Address address){

        try{
            addressService.add(address);
            return new Result(true , MessageConstant.ADD_ADDRESS_SUCCESS);
        }catch (Exception e){

            e.printStackTrace();
            return new Result(false , MessageConstant.ADD_ADDRESS_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = null;
        try{
            pageResult = addressService.findPage(queryPageBean);

        }catch (Exception e){
            e.printStackTrace();
        }
        return pageResult;
    }

    //根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Address address = addressService.findById(id);
            return new Result(true,MessageConstant.QUERY_ADDRESSS_SUCCESS,address);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ADDRESSS_FAIL);
        }

    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Address address){
        try{
            addressService.update(address);
            return new Result(true,MessageConstant.EDIT_ADDRESS_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ADDRESS_FAIL);
        }

    }

    //删除
    @RequestMapping("/delete")
    public Result delete(@RequestParam("id") Integer id,@RequestParam("img") String img){
        try{

            addressService.delete(id);
            //删除成功数据库的数据后再删除七牛云中id的图片
            QiniuUtils.deleteFileFromQiniu(img);
            return new Result(true,MessageConstant.DELETE_ADDRESS_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            //报异常为该地址项仍被预约中//查询信息
            List<Map> list = null;
            try {
                list = addressService.findOrderByAddressId(id);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return new Result(false,MessageConstant.DELETE_ADDRESS_FAIL,list);
        }

    }


}
